/*
 * Copyright (c) 2014, 2015, XIANDIAN and/or its affiliates. All rights reserved.
 * XIANDIAN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 */
package com.xd.cloud.service.core;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xd.cloud.dao.Page;
import com.xd.cloud.dao.core.RoleDao;
import com.xd.cloud.entity.core.Role;

/**
 * 用户角色service
 * 
 * @author 云计算应用与开发项目组
 * @since  V1.0
 * 
 */
@Service
public class RoleService {

	@Autowired
	private RoleDao roleDao;
	
	public Role get(int id)
	{
		return roleDao.get(id);
	}
	
	public boolean remove(int id)
	{
		Role c = roleDao.get(id);
		if (c != null)
		{
			roleDao.delete(c);
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public boolean update(int id,String name,String authority)
	{
		Role r = roleDao.getRoleByname(id,name);
		if (r != null)
		{
			return false;
		}
		else
		{
			Role role = roleDao.get(id);
			role.setName(name);
			role.setAuthority(authority);
			roleDao.update(role);
			return true;
		}
	}
	
	public boolean save(Role c)
	{
		Role r = roleDao.getRoleByname(c.getName());
		if (r == null)
		{
			roleDao.save(c);
			return true;
		}
		return false;
	}
	
	public boolean save(String name,String authority)
	{
		Role r = roleDao.getRoleByname(name);
		if (r == null)
		{
			Role c = new Role();
			c.setName(name);
			c.setAuthority(authority);
			roleDao.save(c);
			return true;
		}
		return false;
	}
	
	public List<Role> getAll()
	{
		return roleDao.getAll();
	}

	public Page getAllRole(int pageNo,int pageSize)
	{
		return roleDao.getAllRole(pageNo, pageSize);
	}
	
	public List<Role> getRoleWithoutAd()
	{
		return roleDao.getRoleWithoutAd();
	}
	
	public boolean updateRoleNameByIdAndName(int id, String name) {
		Role role = roleDao.get(id);
		role.setName(name);
		roleDao.update(role);
		return true;
	}
	
	public Role getRoleBykey(String rolekey)
	{
		return roleDao.getRoleBykey(rolekey);
	}
}
